﻿using TSharp.Core;
using TSharp.Core.Util;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Configuration;
using Common.Logging.Configuration;
using System.Linq;
using Common.Logging;

namespace Wicresot.CoreTests
{


    /// <summary>
    ///这是 ConfigurationHeplerTest 的测试类，旨在
    ///包含所有 ConfigurationHeplerTest 单元测试
    ///</summary>
    [TestClass()]
    public class ConfigurationHeplerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        LogSection GetData()
        {
            var f = new FactoryAdapterCollection();
            var a = new AdapterArgCollection();
            a.Add("configType", "INLINE");
            var intfs = from type in AppDomain.CurrentDomain.GetAssemblies().SelectMany(v => v.GetTypes())
                        where type.IsClass && !type.IsAbstract && typeof(ILoggerFactoryAdapter).IsAssignableFrom(type)
                        select type;
            int i = 0;
            for (var e = intfs.GetEnumerator(); e.MoveNext(); i++)
            {
                f[i] = new FactoryAdapterElement
                {
                    Type = e.Current,
                    Args = a,
                    Name = e.Current.Name,
                };
            }


            var data = new LogSection
            {
                Name = DateTime.Now.ToString(),
                FactoryAdapters = f,
            };
            return data;
        }

        /// <summary>
        ///ConfigurationSaveAs 的测试
        ///</summary>
        [TestMethod()]
        public void ConfigurationSaveAsTest()
        {

            var c = ConfigurationHepler.GetConfigConfiguration().UpdateSection<LogSection>(g => GetData(), "ConfigurationSaveAsTest").SaveAsTSharpFile(null);
            var s = CMD.RunCmd(@"start /d ""{0}""", c.FilePath);
            Console.WriteLine(s);
        }

        /// <summary>
        ///SectionGroupSaveAs 的测试
        ///</summary>
        [TestMethod()]
        public void SectionGroupSaveAsTest()
        {
        }



        /// <summary>
        ///SectionSaveAs 的测试
        ///</summary>
        [TestMethod()]
        public void SectionSaveAsTest()
        {
            Func<ConfigurationSection> section = () =>
            {
                return GetData();
            };
            //string sectionGroupName = "group";
            //string sectionName = "section";

            //ConfigurationHepler.GetConfigConfiguration().UpdateSection<LogSection>(s =>
            //{
            //    return GetData();
            //}, sectionGroupName, sectionName).SaveAsTSharpFile("SectionSaveAsTest");

        }
        /// <summary>
        ///SectionSaveAs 的测试
        ///</summary>
        [TestMethod()]
        public void SectionSaveAsTest1()
        {

            string sectionName = "sectionName";
            ConfigurationHepler.GetConfigConfiguration().UpdateSection<LogSection>(g => GetData(), sectionName, "SectionSaveAsTest1");

        }
    }
}
